package com.wqtz.chechai.widget;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2016/12/12 22:47
 */

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;

import com.wqtz.chechai.R;


public class BadgerTextView extends TextView {
	private Paint mPaint,mPaintText;
	private boolean mShowBadger = false;
	private int mNum = 0;

	public BadgerTextView(Context context) {
		super(context);
		init();
	}

	public BadgerTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public BadgerTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		mPaint = new Paint();
		mPaint.setColor(getResources().getColor(R.color.red));
		mPaint.setStyle(Paint.Style.FILL);
		mPaint.setAntiAlias(true);

		mPaintText = new Paint(mPaint);
		mPaintText.setStyle(Paint.Style.STROKE);
		mPaintText.setColor(getResources().getColor(R.color.white));
	}


	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (mShowBadger) {
			int size = getWidth()/8;
			canvas.drawCircle(getWidth() - size*2, getHeight()/2, 10, mPaint);
			if (mNum>0){
				mPaintText.setTextSize(10*0.75f);
				String txt = String.valueOf(mNum);
				Rect r = new Rect();
				mPaintText.getTextBounds(txt,0,txt.length(),r);
				canvas.drawText(txt,getWidth() - (size*2+r.width()/2),getHeight()/2+(r.height()/2),mPaintText);
			}
		}
	}


	public void showBadger(boolean show) {
		mShowBadger = show;
		invalidate();
	}


	public void setBadgerNumber(int number){
		mNum = number;
		invalidate();
	}
}

